package game.evaluator;

import game.rule.MorrisGameRule;
import game.util.StringUtils;

public class MorrisGameEvaluatorImprovedImpl implements IMorrisGameEvaluator {

	@Override
	public int estimateOpening(String board) {
		int w = StringUtils.charCount(board, 'W'), b = StringUtils.charCount(
				board, 'B');
		int wMills = 0, bMills = 0;
		for (int i = 0; i < board.length(); ++i) {
			if (board.charAt(i) != 'x') {
				continue;
			}
			if (MorrisGameRule.closeMill(i, StringUtils.replace(board, i, 'W'))) {
				wMills++;
			} else if (MorrisGameRule.closeMill(i,
					StringUtils.replace(board, i, 'B'))) {
				bMills++;
			}
		}
		return w + wMills - b - bMills;
	}

	@Override
	public int estimateMidgameEndgame(String board) {
		int w = StringUtils.charCount(board, 'W'), b = StringUtils.charCount(
				board, 'B');
		if (b <= 2) {
			return 10000;
		} else if (w <= 2) {
			return -10000;
		} else if (MorrisGameRule.generateBlackMovesMidgameEndgame(board)
				.size() == 0) {
			return 10000;
		}
		int wMills = 0, bMills = 0;
		for (int i = 0; i < board.length(); ++i) {
			if (board.charAt(i) != 'x') {
				continue;
			}
			if (MorrisGameRule.closeMill(i, StringUtils.replace(board, i, 'W'))) {
				wMills++;
			} else if (MorrisGameRule.closeMill(i,
					StringUtils.replace(board, i, 'B'))) {
				bMills++;
			}
		}
		return 50 * (w + wMills - b - bMills)
				- MorrisGameRule.generateBlackMovesMidgameEndgame(board).size();
	}

}
